package com.hj.security.ui;

import java.util.List;

import com.hj.security.R;
import com.hj.security.dao.BlackNumberDao;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class NumberSecurityActivity extends Activity {
	private ListView lv_number;
	private Button bt_number_add;
	private BlackNumberDao dao;
	private List<String> numbers;
	private NumberAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.number_security);

		dao = new BlackNumberDao(this);
		numbers = dao.findAll();
		lv_number = (ListView) findViewById(R.id.lv_number);
		adapter = new NumberAdapter();
		lv_number.setAdapter(adapter);
		registerForContextMenu(lv_number);

		bt_number_add = (Button) findViewById(R.id.bt_number_add);
		bt_number_add.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				addNumber(null);
			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();
		Intent intent = getIntent();
		String number = intent.getStringExtra("number");
		if(number!=null){
			addNumber(number);
		}
	}
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.number, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.update_number:
			String oldNumber = numbers.get((int)info.id);
			updateNumber(oldNumber);
			break;
		case R.id.delete_number:
			String number = numbers.get((int)info.id);
			dao.delete(number);
			numbers = dao.findAll();
			adapter.notifyDataSetChanged();
			break;

		default:
			break;
		}
		return super.onContextItemSelected(item);
	}

	private void addNumber(String number){
		AlertDialog.Builder builder = new AlertDialog.Builder(
				NumberSecurityActivity.this);
		builder.setTitle("添加黑名单");
		final EditText et_number = new EditText(
				NumberSecurityActivity.this);
		et_number.setInputType(InputType.TYPE_CLASS_PHONE);
		et_number.setHint("请输入黑名单号码");
		if(number!=null){
			et_number.setText(number);
		}
		builder.setView(et_number);
		builder.setPositiveButton("添加",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog,
							int which) {
						String number = et_number.getText().toString()
								.trim();
						if (TextUtils.isEmpty(number)) {
							Toast.makeText(NumberSecurityActivity.this,
									"添加号码不能为空", Toast.LENGTH_LONG)
									.show();
						} else {
							dao.add(number);
							numbers = dao.findAll();
							adapter.notifyDataSetChanged();
						}
					}
				});
		builder.setNegativeButton("取消",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog,
							int which) {
						// TODO Auto-generated method stub

					}
				});
		builder.show();
	}
	private void updateNumber(final String oldNumber) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("更新黑名单");
		final EditText et_new_number = new EditText(this);
		et_new_number.setHint("请输入新的号码");
		builder.setView(et_new_number);
		builder.setPositiveButton("修改", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				String newNumber = et_new_number.getText().toString().trim();
				if(TextUtils.isEmpty(newNumber)){
					Toast.makeText(NumberSecurityActivity.this, "添加号码不能为空", Toast.LENGTH_LONG).show();
				}else{
					dao.update(oldNumber,newNumber);
					numbers = dao.findAll();
					adapter.notifyDataSetChanged();
				}
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		builder.show();
	}

	private class NumberAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return numbers.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return numbers.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView tv_number_item;
			View view;
			if (convertView == null) {
				view = View.inflate(NumberSecurityActivity.this,
						R.layout.number_security_item, null);
			} else {
				view = convertView;
			}
			tv_number_item = (TextView) view.findViewById(R.id.tv_number_item);
			tv_number_item.setText(numbers.get(position));
			return view;
		}
	}
}
